package com.winchain.chainbill.asset.biz.credit.rx;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.acct.transaction.vo.AcctInfo;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.asset.biz.credit.AssetEvent;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.util.StringUtil;

@Component
public class CreditAssetTranBatchSubscriber extends TxSubscriber<AssetEvent<AcctTxRec>> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public void onApplicationEvent(AssetEvent<AcctTxRec> t) {
		String txSn = SystemUtil.getUUID();
		List<AcctTxRec> records = t.getRecords();

		List<AcctTxRec> list = new ArrayList();

		String finId = t.getStringValue("finId");
		String grpId = t.getStringValue("grpId");
		for (AcctTxRec record : records) {
			AcctTxRec tranCredit = getAcctTxRec(record, grpId, finId);

			tranCredit.setTxSn(txSn);
			list.add(tranCredit);
		}
		AcctInfo acctInfo = t.getAcctInfo();
		String source = acctInfo.getAcctKey();
		Map<String, String> result = this.tradeService.fire(source, list);
		for (AcctTxRec txRec : list) {
			txRec.setTxSnHost((String) result.get("tx_sn"));
			t.setValue("txSnHost", txRec.getTxSnHost());
			if ("S".equals(result.get("ret_code"))) {
				this.tradeService.callback(txRec);
				setCreditCode(txRec);
			} else {
				this.tradeService.rollback(txRec);
			}
		}
	}

	private AcctTxRec getAcctTxRec(AcctTxRec txRec, String grpId, String finId) {
		if (StringUtil.isEmpty(finId)) {
			finId = CommonDataUtil.getCurEntId();
		}
		BigDecimal billAmt = txRec.getTxAmt();
		String cusId = txRec.getBizId();
		String abs = "��������";
		String txAcctType = "1";
		String cpAcctType = "1";

		String bizType = txRec.getTxType();

		txRec.setTxId(SystemUtil.getUUID());
		txRec.setCurrCode(AssetCode.XQT.name());
		txRec.setTxChnCode("C");

		AcctBas creditAcct = getAcctBas(finId, txAcctType, null);
		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(creditAcct.getAcctNo(), txRec.getCurrCode(), null, null);

		txRec.setTxAcctNo(creditAcct.getAcctNo());
		txRec.setTxAcctNm(creditAcct.getAcctNm());
		txRec.setTxBankNm(creditAcct.getFiNm());
		txRec.setTxBkpId(txAcctBkp.getBkpId());
		txRec.setTxAmt(billAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxType(bizType);

		txRec.setTxSts("I");

		AcctBas acct = getAcctBas(cusId, cpAcctType, null);
		txRec.setCpAcctNo(acct.getAcctNo());
		txRec.setCpAcctNm(acct.getAcctNm());
		txRec.setCpBankNm(acct.getFiNm());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getCreditAcctBkpByCont(finId, cusId);
		if (cpAcctBkp == null) {
			cpAcctBkp = this.billAcctBlcService.initAcctBkp(acct.getAcctNo(), txRec.getCurrCode(), grpId, null,
					BigDecimal.ZERO);
		}
		txRec.setCpBkpId(cpAcctBkp.getBkpId());

		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);

		txRec.setDataScLvl("B");

		return txRec;
	}

	private void setCreditCode(AcctTxRec tx) {
		String optType = tx.getOptType();
		String txType = tx.getTxType();
		if (("qt_inc".equals(optType)) && ("qt_credit".equals(txType))) {
			String cpBkpId = tx.getCpBkpId();
			String finId = CommonDataUtil.getCurEntId();
			String cusId = tx.getBizId();
			this.billAcctBlcService.setCreditCode(finId, cusId, cpBkpId);
		}
	}
}
